<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-card>
        <el-form-item label="数据集名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入数据集名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="数据集状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="数据集状态" clearable filterable="" style="width: 100%" >
            <el-option v-for="dict in dict.type.sy_dataset_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-card>
    </el-form>

    <el-card style="margin-top:10px;">
      <el-table v-loading="loading" :data="datasetList" border>
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="编码" align="center" prop="code" />
        <el-table-column label="类型" align="center" prop="type" width="120">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sy_dataset_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="120">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sy_dataset_status" v-if="scope.row.status==0" style="color:blue" :value="scope.row.status"/>
            <dict-tag :options="dict.type.sy_dataset_status" v-if="scope.row.status==1" style="color:green" :value="scope.row.status"/>
            <dict-tag :options="dict.type.sy_dataset_status" v-if="scope.row.status==2" style="color:red" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="creator" width="120" />
        <el-table-column label="创建日期" align="center" prop="createTime" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-dropdown :hide-on-click="false">
              <span class="el-dropdown-link" style="color:blue;">
                更多操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleView(scope.row)"><i class="el-icon-document"></i>数据集详情</el-dropdown-item>
                <el-dropdown-item v-if="scope.row.status!=1" style="color:blue;" @click.native="handleUpdate(scope.row)"><i class="el-icon-edit-outline"></i>修改数据集</el-dropdown-item>
                <el-dropdown-item v-if="scope.row.status!=1" style="color:blue;" @click.native="handleUpload(scope.row)"><i class="el-icon-upload"></i>上传数据</el-dropdown-item>
                <el-dropdown-item v-if="scope.row.status!=1" style="color:red;" @click.native="handleDelete(scope.row)"><i class="el-icon-delete-solid"></i>删除数据集</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!--详情-->
    <el-dialog :title="vTitle" :visible.sync="vOpen" width="70%" append-to-body>
      <el-card style="margin-top:-30px">
        <div slot="header" class="clearfix">
          <span style="font-weight:bold;font-size:18px;color:#329dff;">数据集信息</span>
        </div>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="名称" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.name }}
          </el-descriptions-item>
          <el-descriptions-item label="代码" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.code }}
          </el-descriptions-item>
          <el-descriptions-item label="类型" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            <dict-tag :options="dict.type.sy_dataset_type" :value="datasetInfo.type"/>
          </el-descriptions-item>
          <el-descriptions-item label="状态" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            <dict-tag :options="dict.type.sy_dataset_status" :value="datasetInfo.status"/>
          </el-descriptions-item>
          <el-descriptions-item label="创建人员" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.creator }}
          </el-descriptions-item>
          <el-descriptions-item label="创建日期" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ parseTime(datasetInfo.createTime, '{y}-{m}-{d}') }}
          </el-descriptions-item>
          <el-descriptions-item label="发布人员" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.publisher }}
          </el-descriptions-item>
          <el-descriptions-item label="发布日期" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ parseTime(datasetInfo.publishTime, '{y}-{m}-{d}') }}
          </el-descriptions-item>
          <el-descriptions-item label="下架人员" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.updator }}
          </el-descriptions-item>
          <el-descriptions-item label="下架日期" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ parseTime(datasetInfo.updateTime, '{y}-{m}-{d}') }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions :column="1" border style="margin-top:-2px;">
          <el-descriptions-item label="描述" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'90%',color: '#329dff' }">
            {{ datasetInfo.describe }}
          </el-descriptions-item>
          <el-descriptions-item label="备注" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'90%',color: '#329dff' }">
            {{ datasetInfo.remark }}
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <el-card style="margin-top:10px;">
        <div slot="header" class="clearfix">
          <span style="font-weight:bold;font-size:18px;color:#329dff;">数据集对象</span>
        </div>
        <el-table v-if="objectsList.length>0" :data="objectsList" border style="margin-top:10px;">
          <el-table-column label="序号" align="center" type="index" width="55" />
          <el-table-column label="对象名称" prop="name" />
          <el-table-column label="访问路径" prop="path" />
          <el-table-column label="上传人员" align="center" prop="creator" width="120" />
          <el-table-column label="上传日期" align="center" prop="createTime" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit-outline" @click="viewGltfModel(scope.row)">部件编辑</el-button>
              <el-button size="mini" type="text" style="color:red;" icon="el-icon-delete" @click="handleDeleteObject(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-dialog>

    <el-dialog :title="eTitle" :visible.sync="eOpen" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-card style="margin-top:-30px;">
          <div slot="header" class="clearfix">
            <span style="color:#329dff;font-weight:bold;font-size:14px;">数据集信息</span>
          </div>
          <el-row>
            <el-col :span="24">
              <el-form-item label="数据集名称" prop="name">
                <el-input v-model="form.name" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="数据集编码" prop="code">
                <el-input v-model="form.code" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="数据集类型" prop="type">
                <el-select v-model="form.type" placeholder="数据集类型" clearable filterable="" style="width: 100%" >
                  <el-option v-for="dict in dict.type.sy_dataset_type" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="数据集描述" prop="describe">
                <el-input type="textarea" v-model="form.describe" :rows="6"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="form.remark" :rows="6"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
      </el-form>
      <center>
        <div slot="footer" class="dialog-footer" style="margin-top:20px;">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </center>
    </el-dialog>

    <!--数据上传-->
    <el-dialog :title="uTitle" :visible.sync="uOpen" width="70%" append-to-body>
      <el-card style="margin-top:-30px">
        <div slot="header" class="clearfix">
          <span style="font-weight:bold;font-size:18px;color:#329dff;">数据集信息</span>
        </div>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="名称" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.name }}
          </el-descriptions-item>
          <el-descriptions-item label="代码" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.code }}
          </el-descriptions-item>
          <el-descriptions-item label="类型" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            <dict-tag :options="dict.type.sy_dataset_type" :value="datasetInfo.type"/>
          </el-descriptions-item>
          <el-descriptions-item label="状态" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            <dict-tag :options="dict.type.sy_dataset_status" :value="datasetInfo.status"/>
          </el-descriptions-item>
          <el-descriptions-item label="创建人员" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.creator }}
          </el-descriptions-item>
          <el-descriptions-item label="创建日期" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ parseTime(datasetInfo.createTime, '{y}-{m}-{d}') }}
          </el-descriptions-item>
          <el-descriptions-item label="发布人员" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.publisher }}
          </el-descriptions-item>
          <el-descriptions-item label="发布日期" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ parseTime(datasetInfo.publishTime, '{y}-{m}-{d}') }}
          </el-descriptions-item>
          <el-descriptions-item label="下架人员" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ datasetInfo.updator }}
          </el-descriptions-item>
          <el-descriptions-item label="下架日期" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'40%',color: '#329dff' }">
            {{ parseTime(datasetInfo.updateTime, '{y}-{m}-{d}') }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions :column="1" border style="margin-top:-2px;">
          <el-descriptions-item label="描述" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'90%',color: '#329dff' }">
            {{ datasetInfo.describe }}
          </el-descriptions-item>
          <el-descriptions-item label="备注" :labelStyle="{width:'10%','text-align': 'center'}" :contentStyle="{ width:'90%',color: '#329dff' }">
            {{ datasetInfo.remark }}
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <el-card style="margin-top:10px">
        <div slot="header" class="clearfix">
          <span style="font-weight:bold;font-size:18px;color:#329dff;">数据集对象上传</span>
        </div>

        <el-upload
          ref="upload"
          accept=".glb, .gltf"
          class="upload-files"
          :action="fileUrl"
          name="files"
          multiple
          :auto-upload="false"
          :file-list="fileList"
          :show-file-list="false"
          :on-change="handleChange"
        >
          <el-button slot="trigger" type="primary">上传文件</el-button>
        </el-upload>


        <el-table v-if="fileList.length>0" :data="fileList" border style="margin-top:10px;">
          <el-table-column label="待传对象" align="center">
            <el-table-column label="序号" align="center" type="index" width="55" />
            <el-table-column label="对象名称" prop="name" />
          </el-table-column>
        </el-table>

        <el-table v-if="objectsList.length>0" :data="objectsList" border style="margin-top:10px;">
          <el-table-column label="数据集对象" align="center">
            <el-table-column label="序号" align="center" type="index" width="55" />
            <el-table-column label="对象名称" prop="name" />
            <el-table-column label="访问路径" prop="path" />
            <el-table-column label="上传人员" align="center" prop="creator" width="120" />
            <el-table-column label="上传日期" align="center" prop="createTime" width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="160">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit-outline" @click="viewGltfModel(scope.row)">部件编辑</el-button>
                <el-button size="mini" type="text" style="color:red;" icon="el-icon-delete" @click="handleDeleteObject(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>

      </el-card>

      <div slot="footer" class="dialog-footer" v-if="fileList.length>0">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="cancelUpload">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="mTitle" :visible.sync="mOpen" width="100%" append-to-body style="margin-top:-60px;" @close="handleClose">
      <div style="margin-top:-30px;">
        <span style="font-weight:bold;color:#1890FF;font-size:18px;margin-bottom:5px;">部件编辑 ( 数据集： {{ datasetInfo.name }} 【 {{ datasetInfo.code }} 】；数据对象：{{ dataObjectInfo.name }} )</span>
        <model-map v-if="CheckTreeMapVisible" :key="datekey" :datasetId="datasetInfo.uuid" :objectId="dataObjectInfo.uuid" :path="dataObjectInfo.path" style="border-radius:5px;"></model-map>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUploadDataset,editDataset,processDeleteDataset } from "@/api/system/dataset";
import { getDatasetObjectsList,deleteObjectInfo } from "@/api/system/objectinfo";

import { getToken } from "@/utils/auth";
export default {
  name: "DataUpload",
  dicts: ['sy_dataset_status','sy_dataset_type'],
  data() {
    return {
      datekey: null,
      fileList: [],
      // 遮罩层
      loading: true,
      CheckTreeMapVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据集表格数据
      datasetList: [],
      datasetInfo: {},
      objectsList: [],
      dataObjectInfo: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      vTitle: "",
      vOpen: false,
      eTitle: "",
      eOpen: false,
      uTitle: "",
      uOpen: false,
      mTitle: "",
      mOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        type: null,
        describe: null,
        status: null,
        path: null,
        projectIds: null,
        engineerIds: null,
        subEngineerIds: null,
        stationIds: null,
        roleIds: null,
        publishTime: null,
        publishId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      fileUrl: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleClose(){
      this.CheckTreeMapVisible = false;
    },
    viewGltfModel(row){
      this.CheckTreeMapVisible = false;
      this.mTitle = "数据集对象浏览与部件属性编辑";
      this.mOpen = true;
      this.dataObjectInfo = row;
      this.datekey = Date.now();
      this.CheckTreeMapVisible = true;
    },
    handleChange(file){
      this.fileList.push(file);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true;
      listUploadDataset(this.queryParams).then(response => {
        this.datasetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleUpdate(row){
      this.eTitle = "数据集修改";
      this.eOpen = true;
      this.form = row;
    },
    handleDeleteObject(row){
      this.$modal.confirm('是否确认删除 "' + row.name + '" ？').then(function() {
        return deleteObjectInfo({uuid:row.uuid});
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        getDatasetObjectsList({datasetId:this.datasetInfo.uuid}).then(res =>{
          this.objectsList = [];
          this.objectsList = res.data;
        })
      }).catch(() => {});
    },
    handleUpload(row){
      this.fileList = [];
      this.objectsList = [];
      this.uTitle = "数据上传";
      this.uOpen = true;
      this.datasetInfo = row;
      this.fileUrl = process.env.VUE_APP_BASE_API + "/system/dataset/uploadFiles?bucket="+this.datasetInfo.code+"&uuid="+this.datasetInfo.uuid;
      getDatasetObjectsList({datasetId:row.uuid}).then(res =>{
        this.objectsList = res.data;
      })
    },
    handleView(row){
      this.vTitle = "数据集详情";
      this.vOpen = true;
      this.datasetInfo = row;
      this.objectsList = [];
      getDatasetObjectsList({datasetId:row.uuid}).then(res =>{
        this.objectsList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.reset();
      this.eOpen = false;
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        name: null,
        code: null,
        type: null,
        describe: null,
        status: null,
        path: null,
        projectIds: null,
        engineerIds: null,
        subEngineerIds: null,
        stationIds: null,
        roleIds: null,
        publishTime: null,
        publishId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uuid != null) {
            editDataset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.eOpen = false;
              this.getList();
            });
          } else {
            editDataset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.eOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    cancelUpload(){
      this.uOpen = false;
    },
    handleDelete(row){
      this.$modal.confirm('是否确认删除 "' + row.name + '" ？').then(function() {
        return processDeleteDataset(row);
      }).then(() => {
        this.vOpen = false;
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(() => {});
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.uOpen = false;
      this.$refs.upload.clearFiles();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
      this.fileList = [];
      this.uOpen = false;
      this.$modal.msgSuccess("上传成功，同步数据需要一点时间，上传结果上传对象大小而定！");
    }
  }
};
</script>
